"""
Case Type   : 基础功能-SQL语法-DDL-partition
Case Name   : 创建分区表，删除分区对应的 cudesc 表，合理报错
Create At   : 2024/10/14
Owner       : peilinqian
Description :
    step1:创建一张列存分区表;
    step2: 清理已存在用户并创建数据库;
    step3: 普通用户进行dblink创建;
    step4: 系统用户进行dblink创建;
Expect      :
    step1:创建一张列存分区表; expect:成功
    step2: 清理已存在用户并创建数据库; expect:成功
    step3: 普通用户进行dblink创建; expect:创建失败
    step4: 系统用户进行dblink创建; expect:创建成功
History     :
"""
import os
import unittest

from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger
from yat.test import Node


class ListPartitionCase(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(f'----{os.path.basename(__file__)}:初始化----')
        self.pri_dbuser = Node(node='PrimaryDbUser')
        self.pri_sh = CommonSH('PrimaryDbUser')
        self.constant = Constant()
        self.tb_name = 't_ddl_partition_list_142'
        self.err_msg = 'ERROR:  cannot drop relation "?.*", ' \
                       'it is a partition delta/cudesc table'

    def test_main(self):
        step_txt = '----step1:创建一张列存分区表; expect:成功----'
        self.log.info(step_txt)
        sql = f"drop table if exists {self.tb_name};" \
              f"create table {self.tb_name} (id int not null ,name text) " \
              f"with (orientation = column) " \
              f"partition by range (id) (partition p1 values less than(100), " \
              f"partition p2 values less than(200), " \
              f"partition p3 values less than(maxvalue)); "
        result = self.pri_sh.execut_db_sql(sql)
        self.log.info(result)
        self.assertIn(self.constant.CREATE_TABLE_SUCCESS, result,
                      "执行失败" + step_txt)

        step_txt = '----step2:查看分区对应的列存信息;expect:找到分区的 cudesc 表信息 ----'
        self.log.info(step_txt)
        sql = f"select relname,relcudescrelid from pg_partition where " \
              f"parentid=((select oid from pg_class " \
              f"where relname='{self.tb_name}'));"
        result = self.pri_sh.execut_db_sql(sql)
        self.log.info(result)
        self.assertIn('4 rows', result, "执行失败" + step_txt)

        step_txt = '----step3: 查询其中一个分区对应的 cudesc 表; expect:成功----'
        self.log.info(step_txt)
        sql = f"select relname from pg_class where " \
              f"oid = ((select relcudescrelid from pg_partition " \
              f"where relname='p1' and parentid=(select oid from " \
              f"pg_class where relname='{self.tb_name}')));"
        result = self.pri_sh.execut_db_sql(sql)
        self.log.info(result)
        tb_cudesc = result.splitlines()[-2].strip()
        self.assertIn('pg_cudesc_part', tb_cudesc, "执行失败" + step_txt)

        step_txt = '----step3: 删除分区对应的 cudesc 表; expect:失败报错----'
        self.log.info(step_txt)
        sql = f"drop table cstore.{tb_cudesc};"
        result = self.pri_sh.execut_db_sql(sql)
        self.log.info(result)
        self.assertRegex(result, self.err_msg, '执行失败:' + step_txt)

    def tearDown(self):
        self.log.info('----this is tearDown----')
        step_txt = '----step:清理数据; expect:清理成功----'
        self.log.info(step_txt)
        drop_sql = f"drop table if exists {self.tb_name};"
        result = self.pri_sh.execut_db_sql(drop_sql)
        self.log.info(result)

        self.log.info('----teardown断言----')
        self.assertIn(self.constant.DROP_TABLE_SUCCESS, result,
                      "执行失败" + step_txt)

        self.log.info(f'----{os.path.basename(__file__)}:执行完毕----')
